# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=quantlib
_srcname=QuantLib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.36
pkgrel=1
pkgdesc="A free/open-source library for quantitative finance. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.quantlib.org/"
msys2_repository_url="https://github.com/lballabio/QuantLib"
msys2_references=(
  'aur: quantlib'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost")
source=("https://github.com/lballabio/QuantLib/releases/download/v${pkgver}/${_srcname}-${pkgver}.tar.gz"
        "001-fix-install-destination.patch")
sha256sums=('a0eff3d420cc26c21ab8e55d3fd169448abe631a0fbc9f528a6ac444227824fa'
            'b78988a1fc82eb1d4cafead9abc5e685d5618e50df27f7f093c32962fe2915d5')

prepare() {
  cd ${_srcname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-install-destination.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  local -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_CXX_STANDARD=17 \
    -DQL_ENABLE_DEFAULT_WARNING_LEVEL=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DQL_USE_STD_CLASSES=ON \
    -DQL_INSTALL_BENCHMARK=OFF \
    -DQL_BUILD_EXAMPLES=OFF \
    -DQL_INSTALL_EXAMPLES=OFF \
    -DQL_BUILD_TEST_SUITE=OFF \
    -DQL_INSTALL_TEST_SUITE=OFF \
    -DQL_ENABLE_OPENMP=ON \
    ../${_srcname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  # Fix cmake-config files
  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f -name '*.cmake' \
    -exec sed -i -e "s|${WINPREFIX}|\$\{_IMPORT_PREFIX\}|g" {} \;

  install -Dm644 "${srcdir}"/${_srcname}-${pkgver}/LICENSE.TXT "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
